﻿using OF.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OF.Web.admin
{
    public partial class dishadd : System.Web.UI.Page
    {
        BLL.Dish.DishCart dishcartbll = new BLL.Dish.DishCart();
        BLL.Dish.DishAdmin dishbll = new BLL.Dish.DishAdmin();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCart();
                string way = Common.RequestHelp.GetString("way");
                if (way == "edit")
                {
                    BindEdit();
                }
            }
        }

        //绑定数据
        public void BindEdit()
        {
            int id = Common.RequestHelp.GetQueryInt("id");
            Model.Dish dishmodel= dishbll.GetModel(id);
            txtdishname.Text = dishmodel.dishname;
            txtprice.Text = dishmodel.price.ToString();
            txtdesc.Text = dishmodel.describe;
            ddlCart.SelectedValue =dishmodel.catid.ToString();
            cbhot.Checked = dishmodel.hashot;
            cbnew.Checked = dishmodel.hasnew;
            imgphoto.ImageUrl = dishmodel.photo;
            ddlState.SelectedValue = dishmodel.state.ToString();
        }

        public void BindCart()
        {
            DataSet ds = dishcartbll.GetList("sellerid="+WebUtils.GetId());
            ddlCart.DataTextField = "catname";
            ddlCart.DataValueField = "id";
            ddlCart.DataSource = ds;
            ddlCart.DataBind();
            if (ddlCart.Items.Count <= 0) {
                Response.Redirect("dishcart.aspx");
                return;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string way = Common.RequestHelp.GetString("way");
            int id = Common.RequestHelp.GetQueryInt("id");
            string dishname = txtdishname.Text;
            string price = txtprice.Text;
            string desc = txtdesc.Text;
            int cartid =Convert.ToInt32(ddlCart.SelectedValue);
            bool hashot = cbhot.Checked;
            bool hasnew = cbnew.Checked;
            string photo = hfPicName.Value;
            if (way == "edit")//编辑
            {
                Model.Dish dishmodel = dishbll.GetModel(id);
                dishmodel.modifytimes = DateTime.Now;
                dishmodel.catid = cartid;
                dishmodel.describe = desc;
                dishmodel.dishname = dishname;
                dishmodel.hashot = hashot;
                dishmodel.hasnew = hasnew;
                dishmodel.photo = photo;
                dishmodel.price =Convert.ToDecimal(price);
                dishmodel.state = Convert.ToInt32(ddlState.SelectedValue);
                bool r= dishbll.Update(dishmodel);
                if (r == true)
                {
                    //更新缓存
                    OF.BLL.Addr.AreaCache cache = new OF.BLL.Addr.AreaCache();
                    cache.UpdateCache(OF.Common.Enmus.CACHE_TYPE.C_DISH);
                    Common.MessageBox.Show(this, "修改成功");
                }
                else
                {
                    Common.MessageBox.Show(this, "修改失败，请稍后重试");
                }
            }
            else if (way == "add")//增加
            {
                Model.Dish dishmodel = new Model.Dish();
                dishmodel.addtime = DateTime.Now;
                dishmodel.catid = cartid;
                dishmodel.describe = desc;
                dishmodel.dishname = dishname;
                dishmodel.hashot = hashot;
                dishmodel.hasnew = hasnew;
                dishmodel.photo = photo;
                dishmodel.price = Convert.ToDecimal(price);
                dishmodel.state = Convert.ToInt32(ddlState.SelectedValue);
                dishmodel.ordertimes = 0;
                dishmodel.sellerid = WebUtils.GetId(); 
                int r = dishbll.Add(dishmodel);
                if (r > 0)
                {
                    //更新缓存
                    OF.BLL.Addr.AreaCache cache = new OF.BLL.Addr.AreaCache();
                    cache.UpdateCache(OF.Common.Enmus.CACHE_TYPE.C_DISH);
                    Common.MessageBox.Show(this, "添加成功");
                }
                else
                {
                    Common.MessageBox.Show(this, "添加失败，请稍后重试");
                }
            }
        }

        //上传图片
        protected void btnPicSure_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(fileUpLogo.FileName))
            {
                return;
            }
            UploadCommon upload = new UploadCommon();
            string result = upload.MakeThumbnailByMode(fileUpLogo, 2);
            if (!(result.IndexOf("ERROR") > -1 && result.Trim().Length == 7))//上传图片成功
            {
                hfPicName.Value = result;
                this.imgphoto.ImageUrl = result;
                this.lblShow.Text = "";
            }
            else
            {
                this.lblShow.Text = "图片上传失败！";
            }
        }
    }
}